<template>
  <div class="login">
    <img src="../img/login.png" alt />
    <div class="login1">
      <el-form
        :model="ruleForm"
        status-icon
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input type="text" v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button type="primary" @click="registerForm('ruleForm')">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "../utils/request";
import { Notify } from "vant";
import { log } from "util";

export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: ""
      }
    };
  },
  created() {},
  methods: {
    //登录
    submitForm(formName) {
      axios({
        url: "/api/login",
        method: "post",
        data: this.ruleForm
      }).then(res => {
        console.log(res);
        if (res.data.code === 1) {
          window.localStorage.setItem("token", res.data.token);
          console.log(res.data.token);
          this.$router.push("/");
          //同步登录名和头像
          localStorage.setItem("xj", JSON.stringify(res.data.date));
          Notify({ type: "success", message: res.data.msg });
        } else {
          Notify({ type: "danger", message: res.data.msg });
        }
      });
    },
    //注册
    registerForm() {
      axios({
        url: "/api/register",
        method: "post",
        data: this.ruleForm
      }).then(res => {
        if (res.data.code === 2) {
          Notify({ type: "warning", message: res.data.msg });
        } else {
          Notify({ type: "success", message: res.data.msg });
        }
      });
    }
  }
};
</script>

<style lang='scss' scoped>
.login {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  img {
    width: 100%;
    height: 300px;
  }
  .login1 {
    margin-top: 10px;
    width: 330px;
    height: 300px;
  }
}
</style>